﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Simhopp.Common
{
    /// <summary>
    /// The ListOfCompetitions class provides a list of active competitions.
    /// </summary>
    [Serializable]
    public class ListOfCompetitions:ISerializable
    {
        #region FIELD REGION
        private Dictionary<int, String> dic;
        #endregion
        #region CONSTRUCTOR REGION
        /// <summary>
        /// Fills the dictionary with useful information from the competitions in the list.
        /// </summary>
        /// <param name="competitions">A list of active competitions.</param>
        public ListOfCompetitions()
        {
            dic = new Dictionary<int, string>();
        }

        public ListOfCompetitions(List<Competition> competitions)
        {
            dic = new Dictionary<int, string>();
            foreach (var competition in competitions)
            {
                dic.Add(competition.Id, String.Format("{0}, {1} ({2})", competition.Name, competition.Gender, competition.Date));
            }
        }
        #endregion
        public Dictionary<int, String> Dic 
        {
            get
            {
                return dic;
            }
        }

        #region SERIALIZE/DESERIALIZE REGION
        /// <summary>
        /// Adds the class specific information used in the serialization of an object.
        /// </summary>
        /// <param name="info">The class specific information.</param>
        /// <param name="context">Describes the source and destination of a given serialized stream.</param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Dic", dic, typeof(Dictionary<int, String>));
        }
        /// <summary>
        /// Constructor used in the deserialization of an object.
        /// </summary>
        /// <param name="info">The class specific information.</param>
        /// <param name="context">Describes the source and destination of a given serialized stream.</param>
        protected ListOfCompetitions(SerializationInfo info, StreamingContext context)
        {
            dic = (Dictionary<int, String>) info.GetValue("Dic", typeof (Dictionary<int, String>));
        }
        #endregion
    }
}
